﻿using System;
using NCMIS.ObjectModel.MetaData;
using NCMIS.Produce;
using NCMIS.Provider;

namespace NCMIS.Provider.SampleProvider
{
    /// <summary>
    /// Represents a sample implementation of a provider for the Repository Services.
    /// </summary>
    public class SampleRepositoryProvider : RepositoryProviderBase
    {
        /// <summary>
        /// Create an instance of <see cref="NCMIS.Provider.SampleProvider.SampleRepositoryProvider"/>.
        /// </summary>
        public SampleRepositoryProvider()
        {
            SampleFactory.Initialize();
        }

        /// <summary>
        /// Retreives all repositories exposed by this service.
        /// </summary>
        /// <returns>Collection of repositories.</returns>
        public override RepositoryEntry[] GetRepositories()
        {
            return SampleFactory.CreateRepositoryEntries();
        }

        /// <summary>
        /// Returns a specific repository.
        /// </summary>
        /// <param name="repositoryId">The repository id.</param>
        /// <returns>The specified repository entry.</returns>
        public override RepositoryEntry GetRepository(string repositoryId)
        {
            return SampleFactory.CreateRepositoryEntry();
        }

        /// <summary>
        /// Retreives information about the specified CMIS repository.
        /// </summary>
        /// <param name="repositoryId">The repository id.</param>
        /// <returns>RepositoryInfo object for specified repository.</returns>
        public override RepositoryInfo GetRepositoryInfo(string repositoryId)
        {
            return SampleFactory.CreateRepositoryInfo();
        }

        /// <summary>
        /// Retrieves the collection with base types of a repository. Will only retrieve the base types,
        /// independant of parameters supplied. Optional parameters may be specified as null.
        /// </summary>
        /// <param name="repositoryId">The repository id.</param>
        /// <param name="typeId">(optional)</param>
        /// <param name="includePropertyDefinitions">(optional)</param>
        /// <param name="maxItems">(optional)</param>
        /// <param name="skipCount">(optional)</param>
        /// <returns>The specified base types.</returns>
        public override TypeDefinitionList GetTypeChildren(string repositoryId, string typeId, bool includePropertyDefinitions, int? maxItems, int skipCount)
        {
            if (typeId != null) typeId = typeId.Replace("_", ":");
            return SampleFactory.GetChildrenTypes(typeId);
            //return SampleFactory.CreateTypeDefinitionList();
        }

        /// <summary>
        /// Retreives the collection with type descendants for a repository type.
        /// </summary>
        /// <param name="repositoryId">The id of the repository.</param>
        /// <param name="typeId">The type id of an object-type to retreive children for.</param>
        /// <param name="depth">The number of levels of depth in the type hierachy from which to return results. If null is supplied, defaults to value: 2.</param>
        /// <param name="includePropertyDefinitions">If true propertydefintions will be returned for each returned object-type.</param>
        /// <returns>The specified type's descendants.</returns>
        public override TypeContainer[] GetTypeDescendants(string repositoryId, string typeId, int? depth, bool includePropertyDefinitions)
        {
            typeId = typeId.Replace("_", ":");
            return SampleFactory.GetDescendantTypes(typeId);

            //return SampleFactory.CreateTypeContainers();
        }

        /// <summary>
        /// Retreives the type definition for specified type.
        /// </summary>
        /// <param name="repositoryId">The id of the repository.</param>
        /// <param name="typeId">The type id of an object-type to retreive definition for.</param>
        /// <returns>Type definition for specified object-type.</returns>
        public override TypeDefinition GetTypeDefinition(string repositoryId, string typeId)
        {
            return SampleFactory.GetTypeDefinition(typeId);
            //return SampleFactory.CreateTypeDefinition(typeId);
        }
    }
}